import { config } from '../config';

function toQueryString(obj) {
    var str = [];
    for (var p in obj)
        if (obj.hasOwnProperty(p)) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
    return str.join("&");
}

function getUrl(relativePath, params) {
    if (Array.isArray(relativePath)) {
        relativePath = relativePath.join('/');
    }
    if (relativePath[0]=='/') {
        relativePath = relativePath.substring(1);
    }
    var url = [config.serverRootURL, relativePath].join('/');
    if (params) {
        url += '?' + toQueryString(params);
    }
    return url;
}

module.exports = {
    toQueryString: toQueryString,
    getUrl: getUrl
}
